//----------------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------------
Ti.include('include.js');


//----------------------------------------------------------------------------
// Window
//----------------------------------------------------------------------------
var winMdw = Titanium.UI.currentWindow;
winMdw.addEventListener('focus', function(e) {
   log("View 'Medewerkers' actief");
   coverFlowMdw.selected = 0;
   showNameForImage(coverFlowMdw.selected);
});


//----------------------------------------------------------------------------
// View
//----------------------------------------------------------------------------
var namenLijst = ['M. Franssen\nHuisarts', 
   'M.C.P Uitterhoeve-Prins\nHuisarts', 
   'C.N.P. Bruijns\nHuisarts', 
   'P.H.A. le Doux\nHuisarts', 
   'Claudia Cremer\nAssistente', 
   'Debby Smeets-Nijpels\nAssistente', 
   'Mandy Klaassens-Leonard\nAssistente', 
   'Susanne Söffgen-Schulz\nAssistente'];

var mdwImages = ['images/mdw/huisartsen/Margot_medium.png', 
   'images/mdw/huisartsen/Marjolijn_medium.png', 
   'images/mdw/huisartsen/Coen_medium.png', 
   'images/mdw/huisartsen/Philippe_medium.png', 
   'images/mdw/assistentes/Claudia_medium.png', 
   'images/mdw/assistentes/Debby_medium.png', 
   'images/mdw/assistentes/Mandy_medium.png', 
   'images/mdw/assistentes/Susan_medium.png'];

var images = [];
if (isIpad()) {
   for(var i=0,j=mdwImages.length; i<j; i++){
     images.push({
      image: mdwImages[i],
      height: "90%",
      width: "90%"
     });
   };
}
else if (isIphone()) {
   for(var i=0,j=mdwImages.length; i<j; i++){
     images.push({
      image: mdwImages[i],
      height: "50%",
      width: "50%"
     });
   };   
}

//Coverflow view
var coverFlowMdw = Ti.UI.iOS.createCoverFlowView({
   images : images,
   selected : 0
});
coverFlowMdw.addEventListener('change', function(e) {
   showNameForImage(coverFlowMdw.selected);
});

//Buttons
var btnLeft = Titanium.UI.createButton({
   image : '/images/icons/icon_arrow_left.png'
});
btnLeft.addEventListener('click', function(e) {
   var i = coverFlowMdw.selected - 1;
   if (i < 0) {
      i = 0;
   }
   coverFlowMdw.selected = i;
   showNameForImage(coverFlowMdw.selected);
});

var btnRight = Titanium.UI.createButton({
   image : '/images/icons/icon_arrow_right.png'
});
btnRight.addEventListener('click', function(e) {
   var i = coverFlowMdw.selected + 1;
   if (i >= images.length) {
      i = images.length - 1;
   }
   coverFlowMdw.selected = i;
   showNameForImage(coverFlowMdw.selected);
});

//----------------------------------------------------------------------------
// Add UI elements
//----------------------------------------------------------------------------
winMdw.add(coverFlowMdw);

if (isIpad()) {
   var flexSpace = Titanium.UI.createButton({
      systemButton : Titanium.UI.iPhone.SystemButton.FLEXIBLE_SPACE
   });
   winMdw.setToolbar([flexSpace, btnLeft, flexSpace, btnRight, flexSpace]);
} else if (isIphone()) {
   winMdw.leftNavButton = btnLeft;
   winMdw.rightNavButton = btnRight;
}

//----------------------------------------------------------------------------
// Functions
//----------------------------------------------------------------------------

function showNameForImage(imagenr) {
   log("Zoeken naar afbeelding-naam " + imagenr);
   showMessage(namenLijst[imagenr]);
}

function showMessage(msg) {
   messageLabel.text = msg;
   messageView.add(messageLabel);
   winMdw.add(messageView);
}
